package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /*插入订单数据*/
    void save(Orders orders);

    /*根据订单号和用户id查询订单*/
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /*修改订单信息*/
    void update(Orders orders);


    Page<List<Orders>> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    /*取消订单*/
    @Update("update orders set status = 6 where id = #{id}")
    void cancelOrder(@Param("id") Integer id);


    OrderVO queryById(Integer id);


    /**
     * 根据订单ID查询订单明细表
     * @param id
     * @return
     */
    @Select("select * from order_detail where order_id = #{id}")
    List<OrderDetail> queryOrdersById(@Param("id") Integer id);

    Page<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    OrderVO getVoById(Long id);

    @Select("select `status`,count(*) as num from `orders`  where `status` in (2,3,4) group by `status`")
    List<Map> countStatusByGroup();

    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /*根据订单状态和订单时间查询订单*/

    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> queryByOrderTimeAndStatus(Integer status, LocalDateTime orderTime);

    List<Map> queryByParamsWithGroup(Map params);

    List<Map> queryUserCountByParamsWithGroup(Map params);

    List<Map> queryOrderCountByParamsWithGroup(Map params);

    List<GoodsSalesDTO> top10(Map params);

    List<Orders> queryOrdersByParams(Map params);

    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
